package com.house.controller;

import com.house.entity.Users;
import com.house.service.UsersService;
import com.house.util.BaseResult;
import com.house.util.Contant;
import com.house.util.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@RestController
@CrossOrigin(value = "*",allowCredentials ="true")
@RequestMapping("/user/")
public class UsersController {


    @Autowired
    private UsersService usersService;

    @RequestMapping("userReg")      //注册业务
    public BaseResult userReg(Users users) {
        int i = this.usersService.UserService(users);
        if (i > 0) {
            return new BaseResult(BaseResult.RESULT_SUCCESS, Contant.User.successMsg);
        }
        return new BaseResult(BaseResult.RESULT_FAIL, Contant.User.failMsg);
    }

    ;


    @RequestMapping("userLogin")  //登录业务
    public BaseResult userLogin(String name, String password) {
        Users users = this.usersService.userLogin(name,MD5Utils.md5Encrypt(password));

        if (users != null) {
            return new BaseResult(BaseResult.RESULT_SUCCESS, Contant.User.loginSuccessMsg);
        } else {
            return new BaseResult(BaseResult.RESULT_FAIL, Contant.User.loginFailMsg);
        }
    }

    @RequestMapping("userLogins")  //登录业务
    public BaseResult userLogins(String name, String password, HttpSession session) {
        Users users = this.usersService.userLogins(name);

        if (users != null) {
            if (!users.getPassword().equals(MD5Utils.md5Encrypt(password))) {
                return new BaseResult(2, Contant.User.loginFailMg);
            }
                    session.setAttribute("loginInfo",users);
            session.setMaxInactiveInterval(600);
                return new BaseResult(BaseResult.RESULT_SUCCESS, Contant.User.loginSuccessMsg);
        }
         else {
            return new BaseResult(BaseResult.RESULT_FAIL, Contant.User.loginFailMsg);
        }


    }
}